package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * Copyfright(C),2022-2022,复兴元宇科技有限公司
 * FileName:TestValidator
 * Author:yz
 * Date:2022/3/14 11:34 下午
 * Description:
 * History:
 * <author>  <time> <version> <desc>
 * 作者       时间    版本      描述
 */
@Controller
public class TestValidator {
    @GetMapping("/test")
    public String showFrom(Usr usr) {
        return "form";
    }

    @GetMapping("/results")
    public String results() {
        return "results";
    }

    @PostMapping("/test")
    public String checkUsr(@Valid Usr usr, BindingResult bindingResult, RedirectAttributes attr) {
        if (bindingResult.hasErrors()) {
            return "form";
        }
        attr.addFlashAttribute("usr", usr);
        return "redirect:/results";
    }
}
